package com.hunan.api.catv.mq.consumer;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hunan.api.catv.entity.dto.MemberTryWatchLiveRuleDto;
import com.hunan.api.catv.entity.po.MemberRight;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.service.biz.system.SystemDictBiz;
import com.hunan.api.catv.service.biz.tencent_cloud.sms.SmsBiz;
import com.hunan.api.catv.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2020/12/29 0029
 * @package com.hunan.api.catv.mq.consumer
 * @company 湖南有线
 */
@Component
public class MemberTryWatchLiveConsumer {
    private static final String MEMBER_RIGHT_TEMPLATE_ID = "779108";
    private static final String MEMBER_TRY_WATCH_RULE = "member_try_watch_rule";
    public static Logger log = LoggerFactory.getLogger(MemberTryWatchLiveConsumer.class);
    private static final String DEFAULT_RIGHT_CODE = "R000000";
    private static final Integer NORMAL_STATE = 1;
    private static final Integer NOT_DELETED = 1;

    @Autowired
    MemberBiz memberBiz;

    @Autowired
    SystemDictBiz systemDictBiz;

    @Autowired
    SmsBiz smsBiz;

    @Autowired
    RedisUtils redisUtils;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.MEMBER_TRY_WATCH_LIVE_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.MEMBER_TRY_WATCH_LIVE_EXCHANGE, durable = "true", type = "topic"), key = MqConstants.MEMBER_TRY_WATCH_LIVE_ROUTINGKEY))
    public void tasks(String msg) {
        log.info("[增加试看权益收到任务消息");
        MemberTryWatchLiveRuleDto memberTryWatchRuleDto = JSONUtil.toBean(systemDictBiz.getStringByKey(MEMBER_TRY_WATCH_RULE), MemberTryWatchLiveRuleDto.class);
        Boolean flag = false;
        if (memberTryWatchRuleDto != null) {
            flag = Boolean.valueOf(memberTryWatchRuleDto.getNewMemberTryWatchSwitch());
        }
        if (flag) {
            log.info("[增加试看权益收到任务消息,手机号为:{}]", msg);
            MemberVo member = memberBiz.getByPhone(msg);
            MemberRight memberRight = null;
            if (member != null) {
                memberRight = new MemberRight().selectOne(new QueryWrapper<MemberRight>().
                        eq(MemberRight.MEMBER_ID, member.getMemberId()).
                        eq(MemberRight.RIGHT_CODE, DEFAULT_RIGHT_CODE).
                        eq(MemberRight.STATE, NORMAL_STATE).
                        eq(MemberRight.DELETED, NOT_DELETED));
            }
            if (memberRight != null) {
                log.info("用户{}已存在试看权益，不能再次获取");
            } else {
                if (member != null && memberTryWatchRuleDto != null) {
                    memberRight = new MemberRight();
                    memberRight.setMemberId(member.getMemberId());
                    memberRight.setBeginTime(new Date());
                    ZonedDateTime zonedDateTime = LocalDate.now().plusDays(Long.parseLong(memberTryWatchRuleDto.getGlobalTryWatchDays())).atStartOfDay(ZoneId.systemDefault());
                    Date endTime = Date.from(zonedDateTime.toInstant());
                    memberRight.setEndTime(endTime);
                    memberRight.setCreateTime(new Date());
                    memberRight.setUpdateTime(new Date());
                    memberRight.setRightCode(DEFAULT_RIGHT_CODE);
                    memberRight.insert();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    redisUtils.hset("member_try_watch_key", "member" + member.getMemberId(), simpleDateFormat.format(memberRight.getBeginTime() + "-" + memberRight.getEndTime()));
                }
// 发短信     smsBiz.sendSms(msg, MEMBER_RIGHT_TEMPLATE_ID, member.getPhone(), member.getNickName(), memberTryWatchRuleDto.getGlobalTryWatchDays());
            }

        }
    }

}
